package com.example.controller;

import com.example.model.CartItem;
import com.example.model.Order;
import com.example.model.OrderItem;
import com.example.model.User;
import com.example.service.CartItemService;
import com.example.service.OrderItemService;
import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//import tmall.annotation.Auth;
//import tmall.exception.AuthException;
//import tmall.exception.ParameterException;
//import tmall.pojo.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;


@Controller
@RequestMapping("/")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    CartItemService cartItemService;

    @Autowired
    OrderItemService orderItemService;

    @RequestMapping("createOrder")
    public String createOrder(String address, String post, String receiver,
                              String mobile,
                              String userMessage,
                              HttpSession session) throws Exception {
        User user = (User) session.getAttribute("user");
        List<CartItem> cartItems = cartItemService.getCart(user.getId());
//        //简单校验下手机
        Pattern pattern = Pattern.compile("1[0-9]{10}");
        if (!pattern.matcher(mobile).matches()) {
            return ("手机号填写错误");
        }
        Order order = new Order();
        order.setAddress(address);//填写地址
        order.setPost(post);//邮编
        order.setReceiver(receiver);//收件人
        order.setMobile(mobile);//手机号
        order.setUserMessage(userMessage);//留言
        order.setUid(user.getId());
        order.setHidden(0);
        orderService.createOrder(order, cartItems);
        return "redirect:pay?oid=" + order.getOrderCode();
    }
    @RequestMapping("myOrder")
    public String myOrder(HttpSession session, Model model) throws Exception {
        User user = (User) session.getAttribute("user");
        List<Order> orders = orderService.getOrder(user.getId());//根据用户信息查出对应的订单
        //现在你还需要根据订单号，分配不同的订单产品清单。也就是根据多个oid查出多个不同的list
        for (Order o: orders) {
            List<OrderItem> orderItems = orderItemService.findById(o.getOrderCode());//遍历订单集合，分别为他们放入对应的产品列表
            o.setOrderItems(orderItems);//放入对应订单号的商品清单。
        }
        model.addAttribute("orders", orders);//这个是订单列表，负责提供还未完成最后一步的订单
        return "myOrder";
    }

    @RequestMapping("deliver")
    public String deliver(String oid, HttpSession session, Model model) throws Exception {
        Order order = (Order) orderService.getById(oid);
        order.setStatus(4);
        order.setDeliverDate(new Date());
        orderService.updateById(order);
        return "redirect:myOrder";
    }

    @RequestMapping("confirmPay")//有订单号入参
    public String confirmPay(String oid, HttpSession session, Model model) throws Exception {
        Order order = (Order) orderService.getById(oid);
        order.setStatus(5);
        order.setConfirmDate(new Date());
        List<OrderItem>orderItems = orderItemService.findById(oid);
        order.setOrderItems(orderItems);
        model.addAttribute("order", order);
        return "confirmPay";
    }

    @RequestMapping("confirmed")
    public String confirmed(String oid, HttpSession session, Model model) throws Exception {
        Order order = (Order) orderService.getById(oid);
        order.setStatus(6);
        order.setConfirmDate(new Date());
        orderService.updateById(order);
        model.addAttribute("order", order);
        return "confirmed";
    }

    @RequestMapping("deleteOrder")
    @ResponseBody
    public String deleteOrder(String oid, HttpSession session, Model model) throws Exception {
        Order order = (Order) orderService.getById(oid);
        order.setHidden(1);
        orderService.updateById(order);
        return "success";
    }

    @RequestMapping("comment")
    public String comment(Integer oiid, HttpSession session, Model model) throws Exception {
//        OrderItem orderItem = (OrderItem) orderItemService.get(oiid);
//        User user = (User) session.getAttribute("user");
//        checkUser(user, orderItem.getOrder().getUser());
//        model.addAttribute("orderItem", orderItem);
        return "comment";
    }


}
